//
//  AppDelegate.swift
//  个人微博
//
//  Created by 任聪聪 on 2017/5/15.
//  Copyright © 2017年 任孜聪. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        setupAdditional()
        //sleep(2)
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        let wbVC = WBMainViewController()
        window?.rootViewController = wbVC
        window?.makeKeyAndVisible()
        loadAppFile()
        return true
    }
    //模拟从服务器加载数据到本地沙盒
}
//设置应用程序额外信息
extension AppDelegate {
    func setupAdditional() {
        //1.设置SVProgressHUD提示框显示时间
        SVProgressHUD.setMaximumDismissTimeInterval(1)
        //2.设置AFN网络加载指示器模型菊花
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        //3.获取用户授权包括（消息条提醒／消息声音提醒／消息数目显示）
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.carPlay,.badge,.sound]) { (sucess, error) in
                print("获取授权"+(sucess ? "成功":"失败"))
            }
        } else {
            // Fallback on earlier versions
            let userNotifySet = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(userNotifySet)
        }
    }
}
extension AppDelegate {
    fileprivate func loadAppFile(){
        DispatchQueue.global().async {
            //1.从服务器获取数据
            let url = Bundle.main.path(forResource: "MainData.json", ofType: nil)
            //2.将文件转换成数据
            let data = NSData(contentsOfFile: url!)
            //3定位到文件document目录底下
            let docPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            //4.获取本地沙盒地址appendingPathComponent 追加地址
            let dataPath = (docPath as NSString).appendingPathComponent("test.json")
            //5.写入本地沙盒
            data?.write(toFile: dataPath, atomically: true)
            //print("获取服务器数据完毕\(dataPath)")
        }
    }
}

